package com.sooncode.project.focusservice.service;

import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.finder.Page;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.focusservice.model.CourseHour;
import com.sooncode.project.focusservice.model.Member;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CourseHourService {

    public boolean add(Map map){
        if(StringUtils.isEmpty(map.get("memberId").toString())) {
            throw new DomainException("memberId不能为空");
        }

        CourseHour model=new CourseHour();
        model.add(map);
        Member member=new Finder<>(Member.class).byId(map.get("memberId").toString());
        member.sumCourseHour();
        return true;
    }
    public boolean modify(String id,Map map){
        CourseHour model=getById(id);
        model.modify(map);
        return true;
    }
    public boolean delete(String id){
        CourseHour model=getById(id);
        model.delete();

        Member member=new Finder<>(Member.class).byId(model.getMemberId());
        member.sumCourseHour();
        return true;
    }
    public Page<CourseHour> getPage(String memberId,int pageSize,int pageIndex,Map map){
        return new Finder<>(CourseHour.class)
                .byField("memberId",memberId)
                .and(map).page(pageSize,pageIndex);
    }
    public  CourseHour getById(String id){
        return new Finder<>(CourseHour.class).byId(id);
    }
}
